app.controller('BusShiftTplModalCtrl', function ($scope, $filter, $modalInstance, $modal, toaster, param, ajax) {

	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.timeRange = {};
	$scope.stationList = [];
	$scope.typeList = [
		{ id: 1, name: "普通班" },
		{ id: 2, name: "密集班" }
	];
	$scope.statusList = [
		{ id: 1, name: "正常" },
		{ id: 2, name: "暂停" },
		{ id: 3, name: "停用" }
	];
	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};
	if ($scope.param.data.status && $scope.param.data.status > 0) {
		for (var i in $scope.statusList) {
			if ($scope.statusList[i].id != $scope.param.data.status) continue;
			$scope.statusList.selected = $scope.statusList[i];
			break;
		}
	} else {
		$scope.statusList.selected = $scope.statusList[0];
	}

	if ($scope.param.type == "insert") {
		$scope.param.data.startTime = "00:00:00";
		$scope.param.data.endTime = "00:00:00";
		$scope.typeList.selected = $scope.typeList[0];
		$scope.param.data.intervalDay = 0;
	} else if ($scope.param.type == 'update') {
		ajax.call(new Req.GET({
			resource: 'price/bs/' + $scope.param.data.price.id + '/id'
		})).then(function (result) {
			$scope.price = result.result;
			$scope.code = result.code;
			if ($scope.price.type == 0) {
				$scope.priceType = "无折扣";
			} else if ($scope.price.type == 1) {
				$scope.priceType = "等额递减";
			} else if ($scope.price.type == 2) {
				$scope.priceType = "等差递减";
			}
		}, function () { });

		if ($scope.param.data.type == 1) {
			$scope.shiftType = "普通班";
		} else if ($scope.param.data.type == 2) {
			$scope.shiftType = "密集班";
		}
		$scope.startTime = $scope.param.data.startTime;
		$scope.tickets = $scope.param.data.tickets;
		$scope.param.data.startTime = $filter("date")($scope.param.data.startTime, "HH:mm:ss");
		$scope.param.data.endTime = $filter("date")($scope.param.data.endTime, "HH:mm:ss");
		ajax.call(new Req.GET({
			resource: 'route/bs/' + $scope.param.data.route.id + '/station'
		})).then(function (result) {
			$scope.stationList = result.result;
		}, function () { });
	}

	$scope.$watch('routelist.selected', function (newVal, oldVal) {
		if (newVal) {
			ajax.call(new Req.GET({
				resource: 'route/bs/' + newVal.id + '/station'
			})).then(function (result) {
				$scope.stationList = result.result;
			}, function () { });
		}
	});

	//获取组织列表
	$scope.loadOrgList = function () {
		ajax.call(new Req.GET({
			resource: 'organization/3/list'
		})).then(function (result) {
			if (result.code && result.code == 200) {
				$scope.orglist = result.result;
				if ($scope.param.data.organizationId && $scope.param.data.organizationId > 0) {
					for (var j in $scope.orglist) {
						if ($scope.orglist[j].id != $scope.param.data.organizationId) continue;
						$scope.orglist.selected = $scope.orglist[j];
						break;
					}
				}
			}
		}, function () { });
	};
	$scope.loadOrgList();

	//获取线路列表
	$scope.loadRouteList = function () {
		ajax.call(new Req.GET({
			resource: 'route/bs/list'
		})).then(function (result) {
			$scope.routelist = result.result;
		}, function () { });
	};
	$scope.loadRouteList();

	//添加价格规则
	$scope.add = function () {
		if ($scope.routelist.selected == undefined) {
			toaster.pop('warning', '提示', '请选择线路！');
			return;
		}
		$scope.type = 'insert';
		var modalInstance = $modal.open({
			templateUrl: 'template/bus/price/writeModal.html?v=74d294a5e3',
			controller: 'BusPriceModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"type": $scope.type,
						"routeId": $scope.routelist.selected.id,
						"data": $scope.price
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			$scope.price = param.data;
			if ($scope.price.type == 0) {
				$scope.priceType = "无折扣";
			} else if ($scope.price.type == 1) {
				$scope.priceType = "等额递减";
			} else if ($scope.price.type == 2) {
				$scope.priceType = "等差递减";
			}
		});
	};

	$scope.changeTickets = false;
	$scope.changePrice = false;
	$scope.changeStatus = false;
	$scope.changeTime = false;

	//修改价格规则
	$scope.update = function () {
		$scope.type = 'update';
		$scope.current_data = angular.copy($scope.price);
		$modal.open({
			templateUrl: 'template/bus/price/writeModal.html?v=74d294a5e3',
			controller: 'BusPriceModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": $scope.type,
						"routeId": $scope.param.data.route.id,
						"tplId": $scope.param.data.id
					};
				}
			}
		}).result.then(function (param) {
			if (!param.change) return;

			if (param.ids) updater.datalist = param.ids;
			updater.fields = param.data;
			updater.id = param.data.id;
			updater.resource = 'shift/bs/tpl/price/update';
			ajax.call(updater).then(function (result) {
				if (result.code && result.code == 200) {
					$scope.price = param.data;
					if ($scope.price.type == 0) {
						$scope.priceType = "无折扣";
					} else if ($scope.price.type == 1) {
						$scope.priceType = "等额递减";
					} else if ($scope.price.type == 2) {
						$scope.priceType = "等差递减";
					}
					toaster.pop('success', '成功', '修改价格规则成功。');
				} else {
					toaster.pop('error', '错误', '修改价格规则失败！');
				}
			}, function () {
				toaster.pop('error', '错误', '修改价格规则失败！');
			});
		});
	};

	$scope.selectSyncModal = function () {
		return $modal.open({
			templateUrl: 'template/bus/shiftTemplate/updateShift.html?v=f9b9b5f55b',
			controller: 'BusShiftTplUpdateCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"list": $scope.shiftList,
						"tickets": $scope.param.data.tickets,
						"currentTickets": $scope.tickets
					};
				}
			}
		});
	};

	$scope.ok = function () {
		if ($scope.param.type == "insert") {
			if ($scope.myForm.$valid) {
				if ($scope.price == undefined) {
					toaster.pop('warning', '提示', '请添加价格规则！');
					return;
				}
				if ($scope.orglist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择所属单位！');
					return;
				}
				if ($scope.routelist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择线路！');
					return;
				}
				if ($scope.statusList.selected == undefined) {
					toaster.pop('warning', '提示', '请选择状态！');
					return;
				}
				if ($scope.typeList.selected == undefined) {
					toaster.pop('warning', '提示', '请选择类型！');
					return;
				}
				var start = $scope.param.data.startTime;
				var date1 = new Date("1970/1/1 " + start);
				var end = $scope.param.data.endTime;
				var date2 = new Date("1970/1/1 " + end);
				if ($scope.typeList.selected.id == 2 && date1.getTime() >= date2.getTime()) {
					toaster.pop('warning', '提示', '收班时间必须大于开班时间！');
					return;
				}
				$scope.param.data.startTime = date1;
				$scope.param.data.endTime = date2;
				$scope.param.data.status = $scope.statusList.selected.id;
				$scope.param.data.route = $scope.routelist.selected;
				$scope.param.data.type = $scope.typeList.selected.id;
				$scope.param.data.price = $scope.price;
				$scope.param.data.organizationId = $scope.orglist.selected.id;
				$modalInstance.close($scope.param);
			}
		} else if ($scope.param.type == "update") {
			if ($scope.myForm.$valid) {
				if ($scope.orglist.selected == undefined) {
					toaster.pop('warning', '提示', '请选择所属单位！');
					return;
				}
				if ($scope.statusList.selected == undefined) {
					toaster.pop('warning', '提示', '请选择状态！');
					return;
				}
				var start = $scope.param.data.startTime;
				var date1 = new Date("1970/1/1 " + start);
				var end = $scope.param.data.endTime;
				var date2 = new Date("1970/1/1 " + end);
				if ($scope.param.data.type == 2 && date1.getTime() > date2.getTime()) date2 = date1;
				$scope.changeTime = date1.getTime() != $scope.startTime;
				$scope.changeTickets = $scope.tickets != $scope.param.data.tickets;
				$scope.changeStatus = $scope.statusList.selected.id != $scope.param.data.status;
				$scope.param.data.price = $scope.price;
				$scope.param.data.organizationId = $scope.orglist.selected.id;
				$scope.param.data.stationFilter = null; //防止json转化出错 并未改变值
				if (!$scope.changeTickets && !$scope.changeStatus && !$scope.changeTime) {
					$scope.param.data.startTime = date1;
					$scope.param.data.endTime = date2;
					$modalInstance.close($scope.param);
					return;
				}

				ajax.call(new Req.GET({
					resource: 'shift/bs/' + $scope.param.data.id + '/list'
				})).then(function (result) {
					$scope.shiftList = result.result;
					if ($scope.shiftList && $scope.shiftList.length > 0) {
						$scope.selectSyncModal().result.then(function (param) {
							if (param.length > 0) $scope.param.ids = param;

							$scope.param.data.startTime = date1;
							$scope.param.data.endTime = date2;
							$scope.param.data.status = $scope.statusList.selected.id;
							$modalInstance.close($scope.param);
						});
					} else {
						$scope.param.data.startTime = date1;
						$scope.param.data.endTime = date2;
						$scope.param.data.status = $scope.statusList.selected.id;
						$modalInstance.close($scope.param);
					}
				}, function () { });
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});